home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexsunos4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  18.8 KB  |  657 lines

  1. /* Code to do an unexec for Sun O/S 4.1 for a temacs linked -Bdynamic.
  2.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Not synched with FSF. */
  21.  
  22. /*
  23. Created 29-Oct-92 by Harlan Sexton
  24. Tweaked 06-Aug-93 by Dean Michaels to work with sun3.
  25.  */
  26.  
  27. /********************** Included .h Files **************************/
  28.  
  29. #include <config.h>
  30.  
  31. /* I don't understand this, but it's necessary to get some slots in struct exec
  32.    from /usr/include/sys/exec.h when running LCC in strict ANSI mode.  We don't
  33.    need this in K&R mode...
  34.  */
  35. #if defined(__lucid) && defined(__sparc) && !defined(sun)
  36. # define sun 1
  37. #endif
  38.  
  39. #include <stdarg.h>
  40. #include <sys/param.h>
  41. #include <sys/mman.h>
  42. #include <sys/file.h>
  43. #include <sys/stat.h>
  44. #include <sys/types.h>
  45. #include <string.h>
  46. #include <stdio.h>
  47. #include <a.out.h>
  48. #include <unistd.h>
  49. #include <ctype.h>
  50. #include <stab.h>
  51. #include <sys/dir.h>
  52. #include <link.h>
  53.  
  54. /********************** Macros *************************************/
  55.  
  56. #define SYS_ERR \
  57.  ((errno > 0)?((errno < sys_nerr)?(sys_errlist[errno]):\
  58.                "unknown system error"): "unknown error")
  59.  
  60. #define MASK_UP(x,p_of_two) \
  61.  ((((unsigned long) (x)) + ((p_of_two) - 1)) & (~((p_of_two) - 1)))
  62.  
  63. #define MASK_DOWN(x,p_of_two) (((unsigned long) (x)) & (~((p_of_two) - 1)))
  64.  
  65. #ifndef mc68020
  66. #define relocation_info reloc_info_sparc     
  67. #endif
  68.  
  69. /********************** Typedefs and Structs ***********************/
  70.  
  71. struct translation_struct
  72. {
  73.   long txtaddr;
  74.   long txtoff;
  75.   long dataddr;
  76.   long datoff;
  77.   long bssaddr;
  78. };
  79.  
  80. /********************** Function Prototypes/Declarations ***********/
  81.  
  82. static void unexec_error (CONST char *m, int use_errno, ...);
  83. static int unexec_open (char *filename, int flag, int mode);
  84. static caddr_t unexec_mmap (int fd, size_t len, int prot, int flags);
  85. static long unexec_seek (int fd, long position);
  86. static void unexec_read (int fd, long position, char *buf, int bytes);
  87. static void unexec_write (int fd, long position, char *buf, int bytes);
  88. static void unexec_pad (int fd, int bytes);
  89. static void unexec_fstat (int fd, struct stat *statptr);
  90. static void unexec_fchmod (int fd, int mode);
  91. static long unexec_addr_to_offset (long addr, struct translation_struct *ts);
  92. static void copy_relocation_site (struct relocation_info *ri, 
  93.                                   caddr_t from_base_addr, 
  94.                                   caddr_t to_base_addr, 
  95.                                   struct translation_struct *ts);
  96. static void reset_symtab (struct nlist *start, struct nlist *end, 
  97.                           char *strtab, long edata_value, long end_value,
  98.                           int ld_so_table, int shlib_image);
  99. int run_time_remap (char *dummy);
  100.  
  101. /********************** Variables **********************************/
  102.  
  103. /* for reporting error messages from system calls */
  104. extern int sys_nerr;
  105. extern char *sys_errlist[];
  106. extern int errno;
  107. extern int _DYNAMIC;
  108. extern char **environ;             
  109.  
  110. static unsigned long mprotect_bottom_addr;
  111. static unsigned long mprotect_top_addr;
  112.  
  113. static unsigned long sbrk_of_0_at_unexec;
  114.              
  115. /*******************************************************************/
  116.  
  117. static void
  118. unexec_error (CONST char *fmt, int use_errno, ...)
  119. {
  120.   CONST char *err_msg = SYS_ERR;
  121.   va_list args;
  122.  
  123.   fprintf (stderr, "unexec - ");
  124.   va_start (args, use_errno);
  125.   vfprintf (stderr, fmt, args);
  126.   va_end (args);
  127.  
  128.   if (use_errno)
  129.     fprintf (stderr, ": %s", err_msg);
  130.   fprintf (stderr, "\n");
  131.   exit (1);
  132.   return;
  133. }
  134.  
  135. static int
  136. unexec_open (char *filename, int flag, int mode)
  137. {
  138.   int fd;
  139.  
  140.   errno = 0;
  141.  
  142.   fd = open (filename, flag, mode);
  143.  
  144.   if (fd < 0)
  145.     unexec_error ("Failure opening file %s", 1, (void *) filename, 0, 0);
  146.   return fd;
  147. }
  148.  
  149. static caddr_t
  150. unexec_mmap (int fd, size_t len, int prot, int flags)
  151. {
  152.   caddr_t return_val;
  153.  
  154.   unexec_seek (fd, 0);
  155.   errno = 0;
  156.   return_val = mmap (0, len, prot, flags, fd, 0);
  157.  
  158.   if (return_val == (caddr_t) -1)
  159.     unexec_error ("Failure mmap'ing file", 1, 0, 0, 0);
  160.   return return_val;
  161. }
  162.  
  163.  
  164. static long
  165. unexec_seek (int fd, long position)
  166. {
  167.   long seek_value;
  168.  
  169.   if (fd <= 0)
  170.     unexec_error ("No file open in which to seek", 0, 0, 0, 0);
  171.  
  172.   errno = 0;
  173.  
  174.   if (position < 0)
  175.     seek_value = (long) lseek (fd, 0, L_INCR);
  176.   else
  177.     seek_value = (long) lseek (fd, position, L_SET);
  178.  
  179.   if (seek_value < 0)
  180.     unexec_error ("Failed to do a seek to 0x%x in %s", 1,
  181.                   (char *) position, "unexec() output file", 0);
  182.  
  183.   return seek_value;
  184. }
  185.  
  186. static void
  187. unexec_read (int fd, long position, char *buf, int bytes)
  188. {
  189.   int n_read;
  190.   int remains = bytes;
  191.   position = unexec_seek (fd, position);
  192.  
  193.   if (bytes < 0)
  194.     unexec_error ("Attempted read of %d bytes", 0, (char *) bytes, 0, 0);
  195.  
  196.   errno = 0;
  197.  
  198.   while (remains > 0)
  199.     {
  200.       n_read = read (fd, buf, remains);
  201.       if (n_read <= 0)
  202.         unexec_error ("Read failed for 0x%x bytes at offset 0x%x in %s",
  203.                       1, (char *) bytes, (char *) position,
  204.                       "unexec() output file");
  205.       buf += n_read;
  206.       remains -= n_read;
  207.     }
  208.  
  209.   return;
  210. }
  211.  
  212. static void
  213. unexec_write (int fd, long position, char *buf, int bytes)
  214. {
  215.   int n_written;
  216.   int remains = bytes;
  217.   position = unexec_seek (fd, position);
  218.  
  219.   if (bytes < 0)
  220.     unexec_error ("Attempted write of %d bytes in %s",
  221.                   0, (char *) bytes, "unexec() output file", 0);
  222.  
  223.   errno = 0;
  224.  
  225.   while (remains > 0)
  226.     {
  227.       n_written = write (fd, buf, remains);
  228.       if (n_written <= 0)
  229.         unexec_error ("Write failed for 0x%x bytes at offset 0x%x in %s",
  230.                       1, (char *) bytes, (char *) position,
  231.                       "unexec() output file");
  232.       buf += n_written;
  233.       remains -= n_written;
  234.     }
  235.  
  236.   return;
  237. }
  238.  
  239. static void 
  240. unexec_pad (int fd, int bytes)
  241. {
  242.   if (bytes > 0)
  243.     {
  244.       char buf[1024];
  245.       int remaining = bytes;
  246.  
  247.       memset (buf, 0, sizeof (buf));
  248.  
  249.       while (remaining > 0)
  250.         {
  251.           int this_write = (remaining > sizeof(buf))?sizeof(buf):remaining;
  252.           unexec_write (fd, -1, buf, this_write);
  253.           remaining -= this_write;
  254.         }
  255.     }
  256. }
  257.  
  258. static void
  259. unexec_fstat (int fd, struct stat *statptr)
  260. {
  261.   errno = 0;
  262.   if (-1 == fstat (fd, statptr))
  263.     unexec_error ("fstat() failed for descriptor %d", 1, (char *) fd, 0, 0);
  264.   return;
  265. }
  266.  
  267. static void
  268. unexec_fchmod (int fd, int mode)
  269. {
  270.   errno = 0;
  271.   if (-1 == fchmod (fd, mode))
  272.     unexec_error ("fchmod() failed for descriptor %d", 1, (char *) fd, 0, 0);
  273.   return;
  274. }
  275.  
  276. static long
  277. unexec_addr_to_offset (long addr, struct translation_struct *ts)
  278.                              
  279. {
  280.   if ((addr < ts->txtaddr) || (addr >= ts->bssaddr))
  281.     unexec_error ("bad address 0x%x to addr_to_offset()", 
  282.                   0, (char *) addr, 0, 0);
  283.   if (addr >= ts->dataddr)
  284.     return ((long) ((addr - ts->dataddr) + ts->datoff));
  285.   else 
  286.     return ((long) ((addr - ts->txtaddr) + ts->txtoff));
  287. }
  288.  
  289.  
  290. /*
  291.  * "LD.SO" DATA AND SYMBOL TABLE OPERATIONS 
  292.  */
  293.  
  294. static void
  295. copy_relocation_site (struct relocation_info *ri, 
  296.                       caddr_t from_base_addr,
  297.                       caddr_t to_base_addr,
  298.                       struct translation_struct *ts)
  299. {
  300.   long offset = unexec_addr_to_offset (ri->r_address, ts);
  301.   caddr_t from = from_base_addr + offset;
  302.   caddr_t to = to_base_addr + offset;
  303.      
  304. #ifdef mc68020
  305. #define r_type r_length
  306. #endif /* mc68020 */
  307.   switch (ri->r_type)
  308.     {
  309. #ifdef mc68020
  310.     case 0:
  311.       *((char *) to) = *((char *) from);
  312.       break;
  313.     case 1:
  314.       *((short *) to) = *((short *) from);
  315.       break;
  316.     case 2:
  317.       *((long *) to) = *((long *) from);
  318.       break;
  319. #else /* !mc68020 */
  320.     case RELOC_8:
  321.     case RELOC_DISP8:
  322.       *((char *) to) = *((char *) from);
  323.       break;
  324.     case RELOC_16:
  325.     case RELOC_DISP16:
  326.       *((short *) to) = *((short *) from);
  327.       break;     
  328.     case RELOC_LO10:
  329.     case RELOC_13:     
  330.     case RELOC_22:     
  331.     case RELOC_HI22:
  332.     case RELOC_WDISP22:
  333.     case RELOC_WDISP30:
  334.     case RELOC_32:
  335.     case RELOC_DISP32:
  336.     case RELOC_GLOB_DAT:
  337.       *((long *) to) = *((long *) from);
  338.       break;
  339.     case RELOC_JMP_SLOT:
  340.       {
  341.         long *target = (long *) to;
  342.         long *source = (long *) from;
  343.         *target = *source;
  344.         target++;
  345.         source++;
  346.         *target = *source;
  347.         target++;
  348.         source++;
  349.         *target = *source;
  350.       }
  351.       break;
  352. #endif /* !mc68020 */
  353.     default:
  354.       unexec_error ("unknown reloc type %d seen during unexec()",
  355.                     0, (char *) ri->r_type, 0, 0);
  356.       break;
  357.     }
  358.   return;
  359. }
  360.  
  361. static void
  362. reset_symtab (struct nlist *start, struct nlist *end, char *strtab,
  363.               long edata_value, long end_value, int ld_so_table,
  364.               int shlib_image)
  365. {
  366.   struct nlist *tmp = start;
  367.   int found_edata = 0;
  368.   int found_end = 0;
  369.      
  370.   while (tmp < end)
  371.     {
  372.       int type = tmp->n_type;
  373.       int named = (ld_so_table)?1:(tmp->n_un.n_strx);
  374.  
  375.       if ((type == (N_UNDF | N_EXT)) &&
  376.           (tmp->n_value != 0))
  377.         unexec_error ("unexec'ing image has COMMON symbols in it -- we quit!",
  378.                       0, 0, 0, 0);
  379.      
  380.       if (!(type & N_STAB))
  381.         {
  382.           if (!found_edata &&
  383.               (type == (N_EXT | N_DATA)) &&
  384.               named &&
  385.               !strcmp ("_edata", strtab + tmp->n_un.n_strx))
  386.             {
  387.               tmp->n_value = edata_value;
  388.               found_edata = 1;
  389.             }
  390.  
  391.  
  392.           if ((type & N_TYPE) == N_BSS)
  393.             {
  394.               if (!found_end &&
  395.                   (type == (N_EXT | N_BSS)) &&
  396.                   named &&
  397.                   !strcmp ("_end", strtab + tmp->n_un.n_strx))
  398.                 {
  399.                   tmp->n_value = end_value;
  400.                   found_end = 1;
  401.                 }
  402.               else if (type & N_EXT)
  403.                 tmp->n_type = N_DATA | N_EXT;
  404.               else
  405.                 tmp->n_type = N_DATA;
  406.             }
  407.  
  408.           /* the way things are being handled here, having sbrk() in the
  409.              image is fatal for an image linked with shared lib's (although 
  410.              the code could be modified to support it), but this should 
  411.              never happen anyway */
  412.           if (shlib_image &&
  413.               (type == (N_EXT | N_TEXT)) &&
  414.               named &&
  415.               !strcmp ("_sbrk", strtab + tmp->n_un.n_strx))
  416.             unexec_error ("unexec'd shlib image has sbrk() in it -- we quit!",
  417.                           0, 0, 0, 0);
  418.         }
  419.  
  420.       tmp++;
  421.     }
  422.      
  423. extern int getpagesize (void);
  424.  
  425. /*
  426.  * EXPORTED FUNCTIONS 
  427.  */
  428.  
  429. /* this has to be a global variable to prevent the optimizers from
  430.  * assuming that it can not be 0.  
  431. */
  432. static void *dynamic_addr = (void *) &_DYNAMIC;
  433.  
  434. int
  435. unexec (char *new_name, char *old_name,
  436.         unsigned int emacs_edata, unsigned int dummy1, unsigned int dummy2)
  437. {
  438.   /* ld.so data */
  439.   struct link_dynamic *ld = 0;
  440.   struct link_dynamic_2 *ld2 = 0;
  441.   /* old and new state */
  442.   int old_fd;
  443.   int new_fd;
  444.   caddr_t old_base_addr;
  445.   caddr_t new_base_addr;
  446.   struct exec old_hdr;
  447.   struct exec new_hdr;
  448.   struct stat old_buf;
  449.   struct stat new_buf;
  450.   /* some process specific "constants" */
  451.   unsigned long n_pagsiz;
  452.   long page_size = getpagesize ();
  453.   caddr_t plt_end;
  454.   caddr_t current_break = (caddr_t) sbrk (0);
  455.  
  456.   if (!page_size)
  457.     unexec_error ("unexec() failed because we can't get the size of a page!",
  458.                   0, 0, 0, 0);
  459.  
  460.   /* see if this is a -Bdynamic image -- if so, find ld.so structures */
  461.   if (dynamic_addr)
  462.     {
  463.       ld = (struct link_dynamic *) dynamic_addr;
  464.       ld2 = ld->ld_un.ld_2;
  465.       if (ld->ld_version < 2)
  466.         unexec_error ("%s linked with obsolete version of ld -- we quit!",
  467.                       0, old_name, 0, 0);
  468.     }
  469.  
  470.   /* open the old and new files, figuring out how big the old one is
  471.      so that we can map it in */
  472.   old_fd = unexec_open (old_name, O_RDONLY, 0);
  473.   new_fd = unexec_open (new_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
  474.  
  475.   /* setup the header and the statbuf for old_fd */
  476.   unexec_read (old_fd, 0, (char *) &old_hdr, sizeof (old_hdr));
  477.   unexec_fstat (old_fd, &old_buf);
  478.  
  479.  
  480.   /* set up some important constants */
  481.   n_pagsiz = N_PAGSIZ (old_hdr);
  482.   if (dynamic_addr)
  483.     plt_end = (caddr_t) MASK_UP (ld2->ld_plt + ld2->ld_plt_sz, sizeof (double));
  484.   else
  485.     plt_end = (caddr_t) N_DATADDR (old_hdr);
  486.  
  487.   /* never write protect the variable "environ", defined in /lib/crt0.o, and
  488.      set in process.c and callproc.c */
  489.   mprotect_bottom_addr = ((unsigned long) &environ) + sizeof (char **);
  490.   /* never protect ABOVE the end of data emacs_edata specified */
  491.   mprotect_top_addr = MIN (emacs_edata, N_DATADDR (old_hdr) + old_hdr.a_data);
  492.  
  493.   /* Set up the image of the old file */
  494.   old_base_addr = unexec_mmap (old_fd, old_buf.st_size, PROT_READ, MAP_PRIVATE);
  495.   close (old_fd);
  496.  
  497.   /* set up the new exec */
  498.   new_hdr = old_hdr;
  499.   new_hdr.a_data = (((unsigned long) MASK_UP (current_break, n_pagsiz)) - 
  500.                     ((unsigned long) N_DATADDR (old_hdr)));
  501.   new_hdr.a_bss  = 0;
  502.  
  503.   /* set up this variable, in case we want to reset "the break" 
  504.      when restarting */
  505.   sbrk_of_0_at_unexec = ((unsigned long) MASK_UP (current_break, n_pagsiz));
  506.      
  507.   /* Write out the first approximation to the new file. The sizes of
  508.      each section will be correct, but there will be a number of 
  509.      corrections that will need to be made. */
  510.   {
  511.     long old_datoff = N_DATOFF (old_hdr);
  512.     long old_dataddr = N_DATADDR (old_hdr);
  513.     long new_treloff = N_TRELOFF (new_hdr);
  514.     long old_treloff = N_TRELOFF (old_hdr);
  515.     long ld_so_size = ((unsigned long) plt_end) - old_dataddr;
  516.     long real_data_size = current_break - plt_end;
  517.     long pad_size = 
  518.       MASK_UP (current_break, n_pagsiz) - ((unsigned long) current_break);
  519.  
  520.  
  521.     /* First, write the text segment with new header -- copy everything until
  522.        the start of the data segment from the old file, and then go back and 
  523.        write the new header. */
  524.     unexec_write (new_fd, 0, old_base_addr, old_datoff + ld_so_size);
  525.     unexec_write (new_fd, 0, (char *) &new_hdr, sizeof (new_hdr));
  526.  
  527.     /* Copy the rest of the data segment from the running image. */
  528.     unexec_write (new_fd, old_datoff + ld_so_size, 
  529.                   plt_end, real_data_size);
  530.  
  531.     /* pad out the data segment */
  532.     unexec_pad (new_fd, pad_size);
  533.     
  534.     /* Finally, copy the symbol table information from the old file. */
  535.     unexec_write (new_fd, new_treloff,
  536.                   old_base_addr + old_treloff,
  537.                   old_buf.st_size - old_treloff);
  538.   }
  539.      
  540.      
  541.   /* Next, map in the output file so that we can jump around fixing it
  542.      up. We retain the old file so that we can refer to it. */
  543.   unexec_fstat (new_fd, &new_buf);
  544.   new_base_addr = unexec_mmap (new_fd, 
  545.                                MASK_UP (new_buf.st_size, page_size),
  546.                                PROT_READ | PROT_WRITE, MAP_SHARED);
  547.  
  548.  
  549.  
  550.   /* We need to do 2 things. First, make sure that _edata and _end (and
  551.      hence, curbrk) are set to the correct values. At the same time, for
  552.      neatness and to help with debugging, mark all the types of all ld.so
  553.      and nm BSS symbols in the new file to be DATA, and make sure that
  554.      there are no COMMON symbols in the output file, as any references to
  555.      these can lose really big. Second, reset all of the ld.so "relocation
  556.      sites" in the new file to have the values that appear in the old file
  557.      -- the failure to do this was the biggest loser in the old version of
  558.      this code. */
  559.  
  560.   /* STEP 1 */
  561.   {
  562.     /* Reset the regular symbol table first. */
  563.     reset_symtab ((struct nlist *) (new_base_addr + N_SYMOFF(new_hdr)),
  564.                   (struct nlist *) (new_base_addr + N_SYMOFF(new_hdr) +
  565.                                     new_hdr.a_syms),
  566.                   (char *) (new_base_addr + N_STROFF(new_hdr)),
  567.                   N_DATADDR (new_hdr) + new_hdr.a_data,
  568.                   N_BSSADDR (new_hdr) + new_hdr.a_bss,
  569.                   0, !!dynamic_addr);
  570.     /* Now reset the ld.so symbol table. */
  571.     if (dynamic_addr)
  572.       reset_symtab ((struct nlist *) (new_base_addr + ld2->ld_stab),
  573.                     (struct nlist *) (new_base_addr + ld2->ld_symbols),
  574.                     (char *) (new_base_addr + ld2->ld_symbols),
  575.                     N_DATADDR (new_hdr) + new_hdr.a_data,
  576.                     N_BSSADDR (new_hdr) + new_hdr.a_bss,
  577.                     1, !!dynamic_addr);
  578.   }
  579.  
  580.   /* STEP 2 */
  581.   if (dynamic_addr)
  582.     {
  583.       struct translation_struct ts;
  584.       struct relocation_info *tmp = 
  585.         (struct relocation_info *) (old_base_addr + ld2->ld_rel);
  586.       struct relocation_info *end = 
  587.         (struct relocation_info *)(old_base_addr + ld2->ld_hash);
  588.       
  589.       /* set up the structure that we use to translate addresses in the
  590.          old file into file offsets */
  591.       ts.txtaddr = N_TXTADDR (old_hdr);
  592.       ts.txtoff = N_TXTOFF (old_hdr);
  593.       ts.dataddr = N_DATADDR (old_hdr);
  594.       ts.datoff = N_DATOFF (old_hdr);
  595.       ts.bssaddr = N_BSSADDR (old_hdr);
  596.     
  597.       while (tmp < end)
  598.         {
  599.           copy_relocation_site (tmp, old_base_addr, new_base_addr, &ts);
  600.           tmp++;
  601.         }
  602.     }
  603.   
  604.      
  605.   /* get rid of the mmap-ed file space and make the output file 
  606.      executable -- then quit */
  607.   munmap (new_base_addr, MASK_UP (new_buf.st_size, page_size));
  608.   munmap (old_base_addr, MASK_UP (old_buf.st_size, page_size));
  609.   unexec_fchmod (new_fd, 0755);
  610.   close (new_fd);
  611.   return 0;
  612. }
  613.  
  614.  
  615. int
  616. run_time_remap (char *dummy)
  617. {
  618.   long page_size = getpagesize();
  619.   unsigned long base_addr = MASK_UP (mprotect_bottom_addr, page_size);
  620.   unsigned long top_addr = MASK_DOWN (mprotect_top_addr, page_size);
  621.   long len = top_addr - base_addr;
  622.  
  623.   if (!dynamic_addr)
  624.     {
  625.       unsigned long current_sbrk = (unsigned long) sbrk (0);
  626.  
  627.       if (sbrk_of_0_at_unexec < current_sbrk)
  628.     {
  629.       if (sbrk_of_0_at_unexec != 0)
  630.         /* GCC -Wall even catches incorrect printf type errors.
  631.            How utterly cool. */
  632.         fprintf (stderr,
  633.              "Absurd new brk addr = 0x%lx (current = 0x%lx)\n", 
  634.              sbrk_of_0_at_unexec, current_sbrk);
  635.     }
  636.       else
  637.         {
  638.           errno = 0;
  639.           if (brk ((caddr_t) sbrk_of_0_at_unexec))
  640.             fprintf (stderr, "failed to change brk addr to 0x%lx: %s\n", 
  641.                      sbrk_of_0_at_unexec, SYS_ERR);
  642.         }
  643.     }
  644.  
  645.   if (len > 0)
  646.     {
  647.       errno = 0;
  648.       if (mprotect ((caddr_t) base_addr, len, PROT_READ | PROT_EXEC))
  649.         fprintf (stderr, "failed to change protection on data pages: %s\n",
  650.                  SYS_ERR);
  651.     }
  652.              
  653.   return 0;
  654. }
  655.  
  656.